﻿using System;
using DG.Tweening;
using Script.Data;
using UnityEngine;

namespace Script.UI
{
    public class CameraController : MonoSingle<CameraController>
    {
        public GameObject ding;
        private Camera _controlCamera; 
        public Camera ControlCamera
        {
            get
            {
                if (_controlCamera == null)
                {
                    _controlCamera = GameObject.FindWithTag("miniCamera").GetComponent<Camera>();
                }
                return _controlCamera;
            }
        }
        
        private Camera _mainCamera;

        public Camera MainCamera
        {
            get
            {
                if (_mainCamera == null)
                {
                    _mainCamera = GameObject.FindWithTag("main").GetComponent<Camera>();
                }

                return _mainCamera;
            }
        }

        public void OnRecvClickLocationEvent(object data)
        {
            var d = data as Event_LocationSelect;
            var conf = PositionConf.GetConfById(d.LocationID.ToString());
            ding.SetActive(conf.id == "10");
            var conf2 = PositionConf.GetConfById(conf.previewId.ToString());
            ChangeCamera(MainCamera.transform,conf, true);
            ChangeCamera(ControlCamera.transform,conf2, false);
        }
        
        
        public void OnRecvClickLocationEvent(int id)
        {
            var conf = PositionConf.GetConfById(id.ToString());
            ding.SetActive(conf.id == "10");
            var conf2 = PositionConf.GetConfById(conf.previewId.ToString());
            ChangeCamera(MainCamera.transform,conf, true);
            ChangeCamera(ControlCamera.transform,conf2, false);
        }
        

        public void ChangeCamera(Transform trans,PositionConf conf, bool anim)
        {
            if (conf == null)
            {
                return;
            }

            if (anim){
                trans.DOKill();
                var dif = Vector3.Distance(trans.position, conf.position);
                trans.DOMove(conf.position, dif / 100f);
                var dif2 = Vector3.Distance(trans.eulerAngles, conf.rotation);
                trans.DORotateQuaternion(Quaternion.Euler(conf.rotation), dif2/100f);
            }
            else
            {
                trans.position = conf.position;
                trans.eulerAngles = conf.rotation;
            }
        }
    }
}